package platform.common.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import platform.common.exception.InValidDataException;
import platform.common.action.AjaxMessagesJson;
import platform.common.action.BaseAction;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String RESULT_AJAXJSON = "ajaxjson"; // 返回Json对象
	public static final String INVALIDDATAEXCEPTION_CODE = "validateException"; // 验证异常时的code
	private AjaxMessagesJson ajaxMessagesJson; // 设置Json中的值
	protected Logger log = Logger.getLogger(this.getClass());
	
	@SuppressWarnings("unchecked")
	public SessionMap session; 

	public AjaxMessagesJson getAjaxMessagesJson() {
		return ajaxMessagesJson;
	}

	public void setAjaxMessagesJson(AjaxMessagesJson ajaxMessagesJson) {
		this.ajaxMessagesJson = ajaxMessagesJson;
	}


	protected String getLoginName(){
		return (String)getSession().getAttribute("_LOGINNAME_");
	}

	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	/**
	 * 
	 * <br/>方法描述: 用于不抛出异常时，在action中的catch中处理异常信息
	 * @param exception
	 */
	protected void handleException(Exception e){
		log.error(e.getMessage(), e);
		// 封装当前的失败信息到json数据
		// 判断是否是验证异常，如果是那么返回到前台的code码为validateException
		// ，因为验证异常的处理方式是与其他不同的
		// 要求在处理验证方式的异常信息，要与那一个验证项发生错误相对应
		// 如果是发生了关于验证方面的异常信息那就在异常信息中
		if(e instanceof InValidDataException){
			this.getAjaxMessagesJson().setMessage(false, INVALIDDATAEXCEPTION_CODE, e.getMessage());
		}else{
			this.getAjaxMessagesJson().setMessage(false, "", e.getMessage());
		}
	}
	/**
	 * 取出进度信息
	 * @throws Exception
	 */
	public void getProcessInfo() throws Exception{
		HttpSession session = getSession();
		Map<String, String> map = new HashMap<String, String>();
		String operateInfo = "";
		if( null != session.getAttribute("operateInfo") ){
			operateInfo = (String) session.getAttribute("operateInfo");
		}
		map.put("operateInfo", operateInfo);
		String processInfo = "0%";
		if( null != session.getAttribute("processInfo") ){
			processInfo =  session.getAttribute("processInfo").toString();
			if( "100.0%".equals( processInfo ) ){
				processInfo = "-1";
				session.setAttribute("processInfo", 0+"%" );
				session.setAttribute("operateInfo", "" );
			}
		}
		map.put("processInfo", processInfo);
		Gson gson = new Gson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		System.out.println( gson.toJson( map ) );
		out.write( gson.toJson(map) );
		out.flush();
		out.close();
	}
	@SuppressWarnings("unchecked")
	@Override
	public void setSession(Map<String, Object> map) {
		this.session = (SessionMap) map;  
	}

	@SuppressWarnings("unchecked")
	public Map getUserMap() {
		return (Map)session.get("user");
	}
	
	@SuppressWarnings("unchecked")
	public String getUserId() {
		return ((Map)session.get("user")).get("userId").toString();
	}
	
	@SuppressWarnings("unchecked")
	public String getUserCname() {
		return ((Map)session.get("user")).get("userCname").toString();
	}
	
	@SuppressWarnings("unchecked")
	public String getUserRoleName() {
		return ((Map)session.get("user")).get("roleName").toString();
	}
	
	@SuppressWarnings("unchecked")
	public String getUserDepartmentId() {
		return ((Map)session.get("user")).get("departmentId").toString();
	}

	@SuppressWarnings("unchecked")
	public String getUserDepartmentName() {
		return ((Map)session.get("user")).get("departmentName").toString();
	}
	
	@SuppressWarnings("unchecked")
	public String getUserCompanyId() {
		return ((Map)session.get("user")).get("companyId").toString();
	}
	
	@SuppressWarnings("unchecked")
	public String getRoleDFlag() {
		return ((Map)session.get("user")).get("roleDFlag").toString();
	}
	
}
